// 1- 数据请求封装
import axios from "axios";
import {Toast} from "vant";

// 请求详情页数据
function httpForDetail(id) {
  return axios({
      url: `https://m.maizuo.com/gateway?filmId=${id}`,
      headers: {
        'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.2.0","e":"1640782700594797135921153","bc":"110100"}',
        'X-Host': 'mall.film-ticket.film.info'
      }
    })
}

// 请求电影数据列表（热映：type=1； 即将：type=2）; pageNum：多少页
function httpForList(cityId,type,pageNum=1) {
  if (pageNum===1){
    return axios({
      url: `https://m.maizuo.com/gateway?cityId=${cityId}&pageNum=1&pageSize=10&type=${type}`,
      headers: {
        'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.2.0","e":"1640782700594797135921153","bc":"110100"}',
        'X-Host': 'mall.film-ticket.film.list'
      }
    })
  }else {
    const instance = axios.create();
    instance.interceptors.request.use(function (config) {return config;});
    return instance({
      url: `https://m.maizuo.com/gateway?cityId=${cityId}&pageNum=${pageNum}&pageSize=10&type=${type}`,
      headers: {
        'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.2.0","e":"1640782700594797135921153","bc":"110100"}',
        'X-Host': 'mall.film-ticket.film.list'
      }
    })
  }
}

// 请求影院数据列表
function httpForCinema(cityId) {
  return axios({
    url: `https://m.maizuo.com/gateway?cityId=${cityId}&ticketFlag=1`,
    headers: {
      'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.2.0","e":"1640782700594797135921153","bc":"110100"}',
      'X-Host': 'mall.film-ticket.cinema.list'
    }
  })
}

// 请求包含电影的影院数据列表
function httpForFilmsCinema(filmId,cityId) {
  return axios({
    url: `https://m.maizuo.com/gateway/?filmId=${filmId}&cityId=${cityId}`,
    headers: {
      'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.2.0","e":"1640782700594797135921153","bc":"110100"}',
      'X-Host': 'mall.film-ticket.cinema.film-show-cinema'
    }
  })
}

// 请求定位城市
function httpForCity() {
  return axios({
    url: `https://m.maizuo.com/gateway?`,
    headers: {
      'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.2.0","e":"1640782700594797135921153","bc":"110100"}',
      'X-Host': 'mall.film-ticket.city.list'
    }
  })
}
// 请求城市数据列表
function httpForCityLocate() {
  return axios({
    url: `https://m.maizuo.com/gateway?`,
    headers: {
      'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.2.0","e":"1640782700594797135921153","bc":"110100"}',
      'X-Host': 'mall.film-ticket.city.locate'
    }
  })
}

// 请求影院信息
function httpForCinemaInfo(cinemaId) {
  return axios({
    url: `https://m.maizuo.com/gateway/?cinemaId=${cinemaId}`,
    headers: {
      'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.2.0","e":"1640782700594797135921153","bc":"110100"}',
      'X-Host': 'mall.film-ticket.cinema.info'
    }
  })
}
// 请求影院电影展示信息
function httpForCinemaShowFilm(cinemaId) {
  return axios({
    url: `https://m.maizuo.com/gateway/?cinemaId=${cinemaId}`,
    headers: {
      'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.2.0","e":"1640782700594797135921153","bc":"110100"}',
      'X-Host': 'mall.film-ticket.film.cinema-show-film'
    }
  })
}
// 请求电影日程安排列表
function httpForCinemaFilmSchedule(filmId,cinemaId,date) {
  return axios({
    url: `https://m.maizuo.com/gateway/?filmId=${filmId}&cinemaId=${cinemaId}&date=${date}`,
    headers: {
      'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.2.0","e":"1640782700594797135921153","bc":"110100"}',
      'X-Host': 'mall.film-ticket.schedule.list'
    }
  })
}



export default {
  httpForDetail,
  httpForList,
  httpForCinema,
  httpForFilmsCinema,
  httpForCity,
  httpForCityLocate,
  httpForCinemaInfo,
  httpForCinemaShowFilm,
  httpForCinemaFilmSchedule,
}


// 第2种封装方法：将一些重复调用的数据封装起来，如 基准头host
// import axios from "axios";
// const http = axios.create({
//   baseURL: 'https://m.maizuo.com',
//   timeout: 1000,
//   headers: {
//     'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.2.0","e":"1640782700594797135921153"}',
//   }
// })

// export default http




// 在发送请求之前拦截 --- showLoading
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  //在请求被发送之前需要做的事
  // console.log("我要发起数据请求了",config)
  // vant加载提示
  Toast.loading({
    message: '加载中...',
    loadingType: 'spinner',
    forbidClick: false,   // 允许背景点击
    duration: 0,
    closeOnClick: true,   // 是否点击后关闭
  });
  return config;
}, function (error) {
  //请求错误后需要做的事
  return Promise.reject(error);
});

// 在请求成功后拦截 --- hideLoading
// 添加响应拦截器，比.then更早
axios.interceptors.response.use(function (response) {
  // 任何在 2xx 范围内的状态码都会触发这个函数
  // 对响应数据做一些事情
  // 隐藏加载中提示
  Toast.clear()
  return response;
}, function (error) {
  // 任何超出范围的状态码的2XX原因此功能可触发
  //做一些与响应错误
  // 隐藏加载中提示
  Toast.clear()
  return Promise.reject(error);
});


